using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WASM.WebSite.Models;
using WASM.WebSite.Logic;

namespace WASM.WebSite.Controllers
{
	[Authorize()]
	public class TableController : Controller
	{
		public ActionResult List(Guid storageId)
		{
			ViewData["storageId"] = storageId;
			ViewData.Model = TableRepository.Create(storageId).TableList();
			return View();
		}

		public ActionResult Rows(Guid storageId, string storageBoxId)
		{
			ViewData["storageId"] = storageId;
			ViewData["storageBoxId"] = storageBoxId;

			ViewData.Model = TableRepository.Create(storageId).Rows(storageBoxId, 100);
			return View();
		}

		public ActionResult Entity(Guid storageId, string storageBoxId, string partitionKey, string rowKey)
		{
			ViewData["storageId"] = storageId;
			ViewData["storageBoxId"] = storageBoxId;

			ViewData.Model = TableRepository.Create(storageId).GetEntity(storageBoxId, partitionKey, rowKey);
			return View();
		}

		public ActionResult EntityDelete(Guid storageId, string storageBoxId, string partitionKey, string rowKey)
		{
			ViewData["storageId"] = storageId;
			ViewData["storageBoxId"] = storageBoxId;
			TableRepository.Create(storageId).EntityDelete(storageBoxId, partitionKey, rowKey);
			return RedirectToAction("Rows", new { storageId = storageId, storageBoxId = storageBoxId });
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult EntityCreate(Guid storageId, string storageBoxId)
		{
			ViewData["storageId"] = storageId;
			ViewData["storageBoxId"] = storageBoxId;
			return View();
		}

		[AcceptVerbs( HttpVerbs.Post)]
		public ActionResult EntityCreate(Guid storageId, string storageBoxId, TableGenericEntity tableGenericEntity)
		{
			TableRepository.Create(storageId).EntityCreate(storageBoxId, tableGenericEntity);
			return RedirectToAction("Rows", new { storageId = storageId, storageBoxId = storageBoxId });
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult TableCreate(Guid storageId)
		{
			ViewData["storageId"] = storageId;
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult TableCreate(Guid storageId, string storageBoxId)
		{
			TableRepository tr = TableRepository.Create(storageId);
			if(tr.IsTableExist(storageBoxId))
			{
				if (String.IsNullOrEmpty(storageBoxId))
					ModelState.AddModelError("storageBoxId", "Table Name is empty");
				else if (TableRepository.Create(storageId).IsTableExist(storageBoxId))
					ModelState.AddModelError("storageBoxId", String.Format("Table [{0}] alredy exits", storageBoxId));
			}

			if (ModelState.IsValid)
			{
				TableRepository.Create(storageId).TableCreate(storageBoxId);
				return RedirectToAction("Rows", new { storageId = storageId, storageBoxId = storageBoxId });
			}
			else
			{
				return View();
			}
		}

		public ActionResult TableDelete(Guid storageId, string storageBoxId)
		{
			TableRepository.Create(storageId).TableDelete(storageBoxId);
			return RedirectToAction("List", new { storageId = storageId});
		}

	}
}
